package com.dao;

import com.entity.User;
import com.util.DBpool.DBconnection;
import com.util.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import static com.util.GlobalArgs.StringInt_max_min;
import static java.lang.Integer.parseInt;

/**
 * Created by wlc on 2018/2/28.
 */
public class UserDao {
    /**
     * 根据用户名和密码判断用户在数据库中是否存在
     * @param user
     * @return 用户信息 or null
     */
    public static String isUserExist(User user) {
        DBconnection connection = new DBconnection();
        connection.setSheet_name("user");
        ArrayList<ArrayList<String>> user_infors = connection.getDataFromDB();

        for (ArrayList<String> user_infor : user_infors) {
            if ((user_infor.get(1).equals(user.getUsername()) ||  user_infor.get(6).equals(user.getUsername())) && user_infor.get(2).equals(user.getPassword())){
                return StringUtils.join(Arrays.asList(user_infor.toArray()),"wlcniubi");
            }
        }
        return "null";
    }

    /**
     * 根据用户名判断用户在数据库中是否注册
     * @param username
     * @return
     */
    public static String isUserRegister(String username) {
        DBconnection connection = new DBconnection();

        connection.setSheet_name("user");
        ArrayList<String> username_infors = connection.getDataFromFiled(1);

        for (String username_infor : username_infors) {
            if (username_infor.equals(username)){
                return "true";
            }
        }
        return "false";
    }

    /**
     * 添加一个新用户到数据库
     * @param user
     */
    public static void addNewUser(User user){
        DBconnection connection = new DBconnection();
        connection.setSheet_name("user");

        ArrayList<String> id_list = connection.getDataFromFiled(0);
//        取最大id,倒序排序
        id_list.sort(StringInt_max_min);
        int new_id = parseInt(id_list.get(0))+1;
        user.setId(String.valueOf(new_id));
        user.setPermissions("88888888888");
        connection.saveDataToDB(user.userToList());
    }

    /**
     * 修改数据库用户表用户信息
     * @param changeField_Data
     */
    public static void updateUserInformation(ArrayList<String> changeField_Data){
        DBconnection connection = new DBconnection();
        connection.setSheet_name("user");
        String recordField = changeField_Data.get(0).split("wlcniubi")[0];
        String recordData = changeField_Data.get(0).split("wlcniubi")[1];
        for (int i=1; i < changeField_Data.size();i++){
            String changeField = changeField_Data.get(i).split("wlcniubi")[0];
            String changeData = changeField_Data.get(i).split("wlcniubi")[1];
            if (!changeData.equals("null")){
                connection.changeDataToDB(changeField, changeData, recordField, recordData);
            }
        }
    }

    /**
     * 存储用户信息到本地磁盘
     * @param user
     * @param filePath
     * @throws IOException
     */
    public static void saveUserInfor(User user, String filePath) throws IOException {
        ArrayList<String> content = new ArrayList<>();

        content.add(user.getUsername());
        content.add(user.getPassword());
        content.add(user.getRealname());
        content.add(user.getUniversity());
        content.add(user.getDepartment());
        content.add(user.getMail());
        content.add(user.getLogin_time());

        FileUtil.createFile(filePath);
        FileUtil.writeFile(filePath,content);
    }

    /**
     * 从本地磁盘读取用户信息
     * @param filePath
     * @return
     * @throws IOException
     */
    public static ArrayList<String> readUserInfor(String filePath) throws IOException{
        return FileUtil.readFile(filePath);
    }

    /**
     * 存储用户搜索结果到本地磁盘
     * @param search_result
     * @param filePath
     * @throws IOException
     */
    public static void saveUserSearchResult(ArrayList<ArrayList<String>> search_result, String filePath) throws IOException {
        ArrayList<String> content = new ArrayList<>();

        for (ArrayList<String> row : search_result){
            String row_data = StringUtils.join(row.toArray(),"wlcniubi");
            row_data = row_data.replaceAll("\"|'|\r\n|\r|\n|\\s*", "");  //去除数据中的换行
            content.add(row_data);
        }

        FileUtil.createFile(filePath);
        FileUtil.writeFile(filePath,content);
    }

    /**
     * 从本地磁盘获取用户搜索结果
     * @param filePath
     * @return
     * @throws IOException
     */
    public static ArrayList<ArrayList<String>> getUserSearchResult(String filePath) throws IOException {
        ArrayList<String> content = FileUtil.readFile(filePath);
        ArrayList<ArrayList<String>> result = new ArrayList<>();

        for (String row: content){
            result.add(new ArrayList<>(Arrays.asList(row.split("wlcniubi"))));
        }

        return result;
    }
}